package com.iengine.services.api.command.chat;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.iengine.services.api.utils.Constant;
import com.iengine.services.api.utils.DateUtils;
import com.ingine.data.pojo.HReply;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class CommandQueryWeather extends AbstractTxtPhoneApiCommand{
	
	public static final String FIELD_BOX_SN= "boxSN";
	public static final String FIELD_ADDRESS = "Address";
	public static final String FIELD_TIME1= "Time1";
	public static final String FIELD_TIME2= "Time2";
	public static final String FIELD_WEATHER_TYPE= "WeatherType";
	
	@Override
	protected Map formatArgs(Map map, Map nlpParams) {
		HReply reply = getRemoteData(map, nlpParams);
		map=this.getFormatResult(reply,map);
		return map;
	}

	protected Map getFormatResult(HReply reply,Map map){
		map = addValue(map, isDirectReturn(), reply.getContent());
		return map;
	}
	
	public String processQuery(Map map,Map nlpParams){
		Map temp = new HashMap<String, Object>();
		temp.putAll(nlpParams);
		String address = getValue(nlpParams.get(FIELD_ADDRESS),null);
		String time1 = getValue(nlpParams.get(FIELD_TIME1),null);
		String time2 = getValue(nlpParams.get(FIELD_TIME2),null);
//		if(StringUtils.isBlank(address)){
//			String boxSN = getValue(nlpParams.get(FIELD_BOX_SN),null);
//			BoxInfo boxInfo=(BoxInfo) baseDao.queryObject("com.voice.queryBoxSnLocation", boxSN);
//			if(boxInfo !=null && StringUtils.isNoneBlank(boxInfo.getLocationName())){
//				address=boxInfo.getLocationName();
//			}
//		}
	    Map parameterMap=new HashMap();
	    String date=null;
	    if(StringUtils.isNoneBlank(time1)){
	    	date=DateUtils.addDay(new Date(), Integer.parseInt(time1)).replaceAll("-", "");
	    }else{
	    	date=DateUtils.convert(new Date(), DateUtils.DATE_SHORT_FORMAT);
	    }
	    temp.put(FIELD_TIME1, date);
	    temp.put(FIELD_TIME2, time2);
	    if(StringUtils.isNoneBlank(FIELD_ADDRESS)){
	    	temp.put(FIELD_ADDRESS, address);
	    }
	    parameterMap.put(Constant.DATA_TYPE_FIELD,Constant.WEATHER_FIELD);
		parameterMap.put(Constant.PARAMS_DATA_FIELD, temp);
	    return new Gson().toJson(parameterMap);
	}
	
	public Map addValue(Map map, boolean flag, Object value){
		if(value instanceof String){
			List temp = new ArrayList<String>();
			temp.add(value);
			map.put(Constant.RESULT_NLP_VALUE, temp);
		}
		map.put(Constant.RESULT_NLP_RETURN_TYPE, flag);
		return map;
	}

}
